package com.atguigu.stgy.admin.service.impl;

import com.atguigu.stgy.admin.service.ApartmentFeeValueService;
import com.atguigu.stgy.model.dto.ApartmentDTO;
import com.atguigu.stgy.model.entity.ApartmentFacility;
import com.atguigu.stgy.model.vo.FeeValueVO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.stgy.model.entity.ApartmentFeeValue;
import com.atguigu.stgy.admin.mapper.ApartmentFeeValueMapper;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ApartmentFeeValueServiceImpl extends ServiceImpl<ApartmentFeeValueMapper, ApartmentFeeValue> implements ApartmentFeeValueService {

    @Override
    public void batchSaveFeeValue(ApartmentDTO apartmentDTO, Long apartmentInfoId) {
        if(CollectionUtils.isEmpty(apartmentDTO.getFeeValueIds())){
            return;
        }
        List<ApartmentFeeValue> feeValues = apartmentDTO.getFeeValueIds().stream()
                .map(feeId -> {
                    ApartmentFeeValue apartmentFeeValue = new ApartmentFeeValue();
                    apartmentFeeValue.setApartmentId(apartmentInfoId);
                    apartmentFeeValue.setFeeValueId(feeId);
                    return apartmentFeeValue;
                }).collect(Collectors.toList());
        this.saveBatch(feeValues);
    }

    @Override
    public List<FeeValueVO> getFeeValueVOsByApartmentId(Long apartmentId) {
        return baseMapper.selectFeeValueVOsByApartmentId(apartmentId);
    }

    @Override
    public void updateApartmentFeeValues(ApartmentDTO apartmentDTO) {
        this.remove(Wrappers.lambdaQuery(ApartmentFeeValue.class)
                .eq(ApartmentFeeValue::getApartmentId , apartmentDTO.getId()));
        //保存本次提交的配套列表
        this.batchSaveFeeValue(apartmentDTO , apartmentDTO.getId());

    }
}
